#include "map.h"
#include "samplerecord.h"
#include "polyphonychannel.h"
#include "sndfile.h"


Map::Map(size_t bufsize)
{
    for(int i=0; i<sampler::k_max_polyphony; ++i)
        freeChannels_.append(new PolyphonyChannel(bufsize));
}

Map::~Map(){
    qDeleteAll(freeChannels_);
    qDeleteAll(currentChannels_);
}

const SampleRecord *
Map::loadSample(const Note & bottomLeft, const Note & upperRight,
                char root_unused, const QString & fileName)
{
    Q_UNUSED (root_unused);
    gl::log("MAP: bottom left %1", bottomLeft.note);
    gl::log("MAP: upper right %1", upperRight.note);
    Note bl_note_temp =
            upperRight.note < bottomLeft.note ? upperRight : bottomLeft;
    SampleRecord * sr = new SampleRecord(bl_note_temp, upperRight, fileName);
    if(sr == NULL) gl::log("MAP: sample is NULL");
    else gl::log("MAP: sample is loaded");
    sample_table_.append(sr);
    for (int i = bl_note_temp.note; i <= upperRight.note; ++i) {
//        gl::log("MAP: sample is mapped to %1", i);
        map_[i].sample = sr;
    }

    return sr;
}




